import {createRouter, createWebHashHistory, RouteRecordRaw} from 'vue-router'
import { basicRoutes } from './routes';
import type { App } from 'vue';
// 白名单
const WHITE_NAME_LIST: string[] = [];
const getRouteNames = (array: any[]) =>
    array.forEach((item) => {
      WHITE_NAME_LIST.push(item.name);
      getRouteNames(item.children || []);
    });
// 路由放置
getRouteNames(basicRoutes);
console.log(WHITE_NAME_LIST);
//路由创建
export const router = createRouter({
  history: createWebHashHistory(),
  routes: basicRoutes as unknown as RouteRecordRaw[],
  strict: true,
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 路由重置
export function resetRouter() {
  router.getRoutes().forEach((route) => {
    const { name } = route;
    if (name && !WHITE_NAME_LIST.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name);
    }
  });
}
// 路由挂载
export function setupRouter(app: App<Element>) {
  app.use(router);
}
